package cn.fancy.sdk.login.view;

import android.text.TextUtils;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.fancy.sdk.baseview.BaseView;
import cn.fancy.sdk.baseview.IActivityManager;
import cn.fancy.sdk.common.utils.NetworkImpl;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.common.utils.SdkAsyncTask;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.utils.HttpReq;
import cn.fancy.sdk.utils.CommMessage;
import cn.fancy.sdk.utils.TipMessSingle;

public class ResetPassView extends BaseView {
	private IActivityManager activityMgr;
	private Activity ownerActivity;
	private Context mContext;
	private EditText guild_login_username_et;
	private TextView login, callcustom;
	private ImageView guild_passseen, guild_reset_back;
	private SdkAsyncTask<String> resetPassAsyncTask = null;
	private String phonenum;
	
	private String id;
	private String username;

	public ResetPassView(Activity cActivity, IActivityManager activityMgr,
			String uphone,String id,String username) {
		super(cActivity.getBaseContext(), ResUtil.getLayoutId(
				cActivity.getBaseContext(), "guild_resetpass"));
		mContext = cActivity.getBaseContext();
		this.activityMgr = activityMgr;
		this.id = id;
		this.username = username;
		ownerActivity = cActivity;
		phonenum = uphone;
		initView();
	}

	private void initView() {
		guild_login_username_et = (EditText) this.findViewById(ResUtil.getId(
				mContext, "guild_login_username_et"));
		guild_login_username_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				String editable = guild_login_username_et.getText()
						.toString();
				String regEx = "[^a-zA-Z0-9]"; // 只能输入字母或数字
				Pattern p = Pattern.compile(regEx);
				Matcher m = p.matcher(editable);
				String str = m.replaceAll("").trim(); // 删掉不是字母或数字的字符
				if (!editable.equals(str)) {
					guild_login_username_et.setText(str); // 设置EditText的字符
					guild_login_username_et.setSelection(str.length()); // 因为删除了字符，要重写设置新的光标所在位置
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});

		login = (TextView) this.findViewById(ResUtil.getId(mContext, "login"));
		callcustom = (TextView) this.findViewById(ResUtil.getId(mContext,
				"callcustom"));
		guild_passseen = (ImageView) this.findViewById(ResUtil.getId(
				mContext, "guild_passseen"));
		guild_reset_back = (ImageView) this.findViewById(ResUtil.getId(
				mContext, "guild_reset_back"));

		login.setOnClickListener(this);
		callcustom.setOnClickListener(this);
		guild_passseen.setOnClickListener(this);
		guild_passseen.setSelected(false);
		guild_reset_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		if (vid == ResUtil.getId(mContext, "login")) {
			resetRequest();
		} else if (vid == ResUtil.getId(mContext, "guild_passseen")) {
			Log.e("clickguild_passseen", "clickguild_passseen");
			if (guild_passseen.isSelected()) {
				guild_passseen.setSelected(false);
				guild_passseen.setBackgroundResource(ResUtil.getDrawableId(
						mContext, "guild_eye_off"));
				guild_login_username_et
						.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				guild_passseen.setSelected(true);
				guild_passseen.setBackgroundResource(ResUtil.getDrawableId(
						mContext, "guild_eye_on"));
				guild_login_username_et
						.setInputType(InputType.TYPE_CLASS_TEXT
								| InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
		} else if (vid == ResUtil.getId(mContext, "guild_reset_back")) {
			activityMgr.NewpopViewFromStack();
		}
	}

	private void resetRequest() {
		if (!NetworkImpl.isNetworkConnected(getContext())) {
			Toast.makeText(mContext, TipMessSingle.ten_networkerr,
					Toast.LENGTH_LONG).show();
			return;
		}
		if (guild_login_username_et.getText().toString().length() < 6) {
			Toast.makeText(mContext, TipMessSingle.two_passtooshort,
					Toast.LENGTH_LONG).show();
			return;
		}
		final Map<String, String> params = GuildBaseInfo.getInstance().resetPassParam(mContext, username,
			guild_login_username_et.getText().toString(), "set_pwd");
		resetPassAsyncTask = new SdkAsyncTask<String>() {

			@Override
			public Activity getOwnerActivity() {
				return ownerActivity;
			}

			@Override
			protected String doInBackground() {
				Log.e("resetpass", "resetpass" + params.toString());
				return HttpReq.doGuildRequest(params);
			}

			@Override
			protected void onPostExecute(String rst) {
				activityMgr.cancelWaitingDialog();
				Log.e("resetpassrst", "resetpassrst" + rst);
				if (TextUtils.isEmpty(rst)) {
					Toast.makeText(mContext, TipMessSingle.ten_networkerr, Toast.LENGTH_LONG).show();
					return;
				}
					try {
						JSONObject json = new JSONObject(rst);
						String state = json.getString("state");
						JSONObject jsonState = new JSONObject(state);
						
						int code = jsonState.getInt("code");
						String msgmsg = jsonState.optString("msg");
						Toast.makeText(getContext(), msgmsg, Toast.LENGTH_LONG).show();
						if (code != 1) {
							return;
						}

						CommMessage.sFindPass = guild_login_username_et.getText().toString();
						activityMgr.NewpopViewFromStack();
						activityMgr.NewpopViewFromStack();// 去除第一个LoginView
						activityMgr.pushViewToStack(new LoginView(ownerActivity, activityMgr));
					} catch (JSONException e) {
						e.printStackTrace();
					}
			}

			@Override
			protected void onCancelled() {
			}
		};
		resetPassAsyncTask.execute();
		activityMgr.showWaitingDialog();
	}
}
